package me.sdevil507.org.controller;

import lombok.extern.slf4j.Slf4j;
import me.sdevil507.org.dto.ExpertSearchDto;
import me.sdevil507.org.dto.TempExpertDto;
import me.sdevil507.org.dto.TempExpertParamsDto;
import me.sdevil507.org.mapper.TempExpertMapper;
import me.sdevil507.org.po.OrgUserRelPo;
import me.sdevil507.org.po.TempExpertPo;
import me.sdevil507.org.service.InviteURLProperties;
import me.sdevil507.org.service.TempExpertService;
import me.sdevil507.supports.helper.Md5PasswordHelper;
import me.sdevil507.supports.jpa.PageConvertCallBack;
import me.sdevil507.supports.jpa.PageResultDTO;
import me.sdevil507.supports.jpa.PageResultGenerator;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import me.sdevil507.supports.shiro.helper.OrgAccountHelper;
import me.sdevil507.supports.status.ApiStatusCode;
import me.sdevil507.supports.util.RegularExpressionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: tips_back_end
 * @description:
 * @author: kzl
 * @create: 2020-08-05 14:46
 **/
@RestController
@RequestMapping("/api/org/texpert")
@Slf4j
public class TempExpertController {

    @Autowired
    private TempExpertService tempExpertService;

    @Autowired
    private Md5PasswordHelper md5PasswordHelper;

    @Autowired
    private TempExpertMapper tempExpertMapper;

    @Autowired
    private InviteURLProperties inviteURLProperties;

    /**
     * 获取用户个人信息
     *
     * @return 个人信息 修改个人信息
     */
    @GetMapping("/info")
    public ApiResultDTO info(@RequestBody TempExpertDto dto) {
        String phoneNumber = dto.getPhoneNumber();
        TempExpertPo tempExpertPo = tempExpertService.readOneByPhoneNumber(phoneNumber);
        TempExpertDto tempExpertDto = tempExpertMapper.po2dto(tempExpertPo);
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), tempExpertDto);
    }

    /**
     * 根据手机号获取用户信息
     *
     * @param dto 入参手机号
     * @return 信息
     */
    @PostMapping("/readByPhoneNumber")
    public ApiResultDTO readByPhoneNumber(@RequestBody TempExpertDto dto) {
        TempExpertPo tempExpertPo  = tempExpertService.readOneByPhoneNumber(dto.getPhoneNumber());
        Map<String, Object> result = new HashMap<>(2);
        if (tempExpertPo != null) {
            TempExpertDto tempExpertDto = tempExpertMapper.po2dto(tempExpertPo);
            result.put("type", true);
            result.put("info", tempExpertDto);
        } else {
            result.put("type", false);
            result.put("info", null);
        }
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), result);
    }


    /**
     * 删除用户
     *
     * @param dto 参数
     * @return 执行反馈
     */
    @PostMapping("/delete")
    public ApiResultDTO delete(@RequestBody TempExpertDto dto) {
        tempExpertService.delete(dto.getId());
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }


    /**
     * 添加临时专家用户
     *
     * @param dto 用户dto
     * @return 执行反馈
     */
    @PostMapping("/create")
    public ApiResultDTO create(@RequestBody TempExpertDto dto) {
        // 查询用户是否存在
        try {
            Long orgId = OrgAccountHelper.getOrgId();
            dto.setOrgID(orgId);
            //过滤空格
            dto.setRealName(RegularExpressionUtil.filterSpace(dto.getRealName()));
            //前台已经判断是否存在
//            if(tempExpertService.realNameExisted(dto.getRealName(),dto.getId(),orgId)){
//                return ApiResultGenerator.create(ApiStatusCode.ACCOUNT_EXISTED.getCode(), ApiStatusCode.ACCOUNT_EXISTED.getDescription());
//            }
            TempExpertPo tempExpertPo = tempExpertService.create(tempExpertMapper.dto2po(dto));
            return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), tempExpertPo.getId());
        }catch (Exception  e){
            return ApiResultGenerator.create(ApiStatusCode.ERROR.getCode(), ApiStatusCode.ERROR.getDescription());
        }
    }


    /**
     * 分页获取信息列表
     *
     * @param dto 入参参数
     * @return 分页数据
     */
    @SuppressWarnings("Convert2Lambda")
    @RequestMapping(value = "/readAllByPage", method = RequestMethod.POST)
    @ResponseBody
    public ApiResultDTO readAllByPage(@RequestBody TempExpertParamsDto dto) {
        dto.getSearchDto().setOrgID(OrgAccountHelper.getOrgId());
        Page<TempExpertPo> pagePos = tempExpertService.readAllByPage(dto.getSearchDto(), dto.getPageDto(), dto.getSortDto());
        PageResultDTO<TempExpertDto> pageResultDto = PageResultGenerator.convert(pagePos, new PageConvertCallBack<TempExpertDto, TempExpertPo>() {
            @Override
            public List<TempExpertDto> exec(List<TempExpertPo> list) {
                return tempExpertMapper.pos2dtos(list);
            }
        });
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), pageResultDto);
    }

    /**
     * 获取待邀请临时专家
     *
     * @param dto 入参参数
     * @return 分页数据
     */
    @SuppressWarnings("Convert2Lambda")
    @RequestMapping(value = "/getInvited", method = RequestMethod.POST)
    @ResponseBody
    public ApiResultDTO getInvited(@RequestBody TempExpertParamsDto dto) {
        dto.getSearchDto().setOrgID(OrgAccountHelper.getOrgId());
        Page<TempExpertPo> pagePos = tempExpertService.getInvited(dto.getSearchDto(), dto.getPageDto(), dto.getSortDto());
        PageResultDTO<TempExpertDto> pageResultDto = PageResultGenerator.convert(pagePos, new PageConvertCallBack<TempExpertDto, TempExpertPo>() {
            @Override
            public List<TempExpertDto> exec(List<TempExpertPo> list) {
                return tempExpertMapper.pos2dtos(list);
            }
        });
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), pageResultDto);
    }

    /**
     * 生成邀请专家ur
     *
     * @param dto 入参参数
     * @return 分页数据
     */
    @RequestMapping(value = "/getInviteUrl", method = RequestMethod.POST)
    @ResponseBody
    public ApiResultDTO getInviteUrl(@RequestBody TempExpertParamsDto dto) {
         String url=inviteURLProperties.getUrl()+"/"+dto.getSearchDto().getId();
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), url);
    }


    /**
     * 获取所有的专家信息
     * @return 专家数据
     */
    @SuppressWarnings("Convert2Lambda")
    @RequestMapping(value = "/readAllExp", method = RequestMethod.POST)
    @ResponseBody
    public ApiResultDTO readAllExp(@RequestBody TempExpertParamsDto dto) {

        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), tempExpertService.findAllExpertOfOrg());
    }

    /**
     * 注册用户
     *
     * @param dto 入参参数
     * @return 分页数据
     */
   /* @RequestMapping(value="/register", method = RequestMethod.POST)
    @ResponseBody
    public ApiResultDTO register(@RequestBody TempExpertDto dto) {

        //注册用户
        try {
            tempExpertService.register(dto);
        } catch (PhoneNumberExistedException e) {
            return ApiResultGenerator.create(ApiStatusCode.ACCOUNT_EXISTED.getCode(), ApiStatusCode.ACCOUNT_EXISTED.getDescription());
        }

        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }*/


}
